package org.example.controller;

import org.example.model.address.AddressBook;
import org.example.model.common.dtos.ResponseResult;
import org.example.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 地址管理
 */
@CrossOrigin
@RestController
@RequestMapping("/address-book")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 添加地址
     */
    @PostMapping("/address-book")
    public ResponseResult AddAddressBook(@RequestBody AddressBook addressBook) {
        return ResponseResult.okResult(addressBookService.InsertAddressBook(addressBook));
    }

    /**
     * 根据token中的用户id获取地址列表
     */
    @GetMapping("/address-list")
    public ResponseResult SelectAddressBooksByUserId() {
        return ResponseResult.okResult(addressBookService.SelectAddressBooksByUserId());
    }

    /**
     * 根据id查询地址详情(原始数据)
     */
    @GetMapping("/address-book/{id}")
    public ResponseResult SelectAddressBookById(@PathVariable int id) {
        return ResponseResult.okResult(addressBookService.SelectAddressBookById(id));
    }

    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping("/address-book")
    public ResponseResult UpdateAddressBookById(@RequestBody AddressBook addressBook) {
        return ResponseResult.okResult(addressBookService.UpdateAddressBookById(addressBook));
    }

    /**
     * 根据id删除地址
     */
    @DeleteMapping("/address-book/{id}")
    public ResponseResult DeleteAddressBookById(@PathVariable int id) {
        return ResponseResult.okResult(addressBookService.DeleteAddressBookById(id));
    }


}
